package com.sys.controller;

import com.sys.utils.WeChatUtils;
import com.bhudy.exception.SysException;
import com.bhudy.exception.SysExceptionCode;
import com.bhudy.utils.ResponseResult;
import com.sys.utils.ShiroUtils;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.dom.DOMSource;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2020/3/4/004.
 */
@RestController
public class WeChatController {

    /**
     * 小程序登录接口
     *
     * @param code
     * @return
     */
    @GetMapping(value = "/api/v1/wechat/login")
    public ResponseResult login(String code, Long userId, String url) {
        return WeChatUtils.login(code, userId, url);
    }

    /**
     * 小程序绑定账号接口
     *
     * @param json
     * @return
     */
    @PostMapping(value = "/api/v1/wechat/bind", produces = "application/json;charset=UTF-8")
    public ResponseResult bind(@RequestBody String json) {
        return WeChatUtils.bind(json);
    }

    /**
     * 验证是否登陆
     * @return
     */
    @RequestMapping(value = "/api/v1/wechat/checkLogin", method = RequestMethod.GET)
    public ResponseResult checkWxLogin() {
        return ResponseResult.printMapJson(0, ShiroUtils.getUser() != null);
    }

    /**
     * 生成微信公众号带参数二维码
     *
     * @return
     */
    @GetMapping(value = "/api/v1/wechat/createTasQrcode")
    public ResponseResult createTasQrcode() {
        if (ShiroUtils.getUserId() == null) throw new SysException(SysExceptionCode.CODE_40043);
        return ResponseResult.printMapJson(WeChatUtils.createTasQrcode());
    }

    /**
     * 生成微信小程序带参数二维码
     *
     * @return
     */
    @GetMapping(value = "/api/v1/wechat/createAppletAqrCode")
    public ResponseResult createAppletAqrCode() {
        if (ShiroUtils.getUserId() == null) throw new SysException(SysExceptionCode.CODE_40043);

        String param = "?url=/pages/index/index&userId=" + ShiroUtils.getUserId();
        return ResponseResult.printMapJson(WeChatUtils.createAppletAqrCode("pages/togo/togo" + param));
    }

    /**
     * 微信服务器绑定接口
     *
     * @param signature
     * @param timestamp
     * @param nonce
     * @param echostr
     * @return
     */
    @GetMapping(value = "/api/v1/wechat/receiveEventPush")
    public Object checkReceiveEventPush(String signature, String timestamp, String nonce, String echostr) {
        if (WeChatUtils.checkSignature(signature, timestamp, nonce)) {
            return echostr;
        } else {
            return "";
        }
    }

    /**
     * 接收微信官方推送数据
     *
     * @param domSource
     * @return
     */
    @PostMapping(value = "/api/v1/wechat/receiveEventPush", consumes = {"application/xml", "text/xml"}, produces = {"application/xml;charset=utf-8", "text/xml;charset=utf-8"})
    public Object receiveEventPush(@RequestBody DOMSource domSource) {
        return WeChatUtils.receiveEventPush(domSource);
    }


    /**
     * 微信发送通知测试接口
     *
     * @return
     */
    @GetMapping(value = "/api/v1/test/testSendWechatTas")
    public ResponseResult testSendWechatTas() {
        Map<String, Object> map = new HashMap<>();
        map.put("first", "您好，您有一条工单消息");
        map.put("keyword1", "test00000001");
        map.put("keyword2", "test00000002");
        map.put("remark", "详细信息请登录网站或者点击详情查看");
        WeChatUtils.sendTasWorkOrder("e1b5652f-0d15-4778-a4f5-e7c279d1fae4", map);

        return ResponseResult.printMapJson();
    }


    @GetMapping(value = "/api/v1/test/getOpenIdListMap")
    public Object getOpenIdListMap() {
        return WeChatUtils.getOpenIdListMap();
    }


    @GetMapping(value = "/api/v1/test/getUserInfo")
    public Object getUserInfo(String openId) {
        return WeChatUtils.getUserInfo(openId, false);
    }

}
